﻿package org.openChart.meta.manager 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.text.StyleSheet;
	import flash.net.URLVariables
	import flash.events.IOErrorEvent
	import flash.net.URLRequest
	import org.openChart.meta.Style;
	
	/**
	 * Load the default style defination file. The file format likes the css file but they are not one thing.
	 * 
	 * @author Chongyuan
	 */
	public class CSSLoader extends EventDispatcher
	{
		
		public static const chart:String = ".chart_2d";
		public static const chart_title:String = ".chart_title";
		public static const default_element_style:String = ".default_element_style";
		public static const default_pie_style:String = ".default_pie_style";
		
		public static const axis_2d:String = ".axis_2d";
		public static const axis_2d_title:String = ".axis_2d_title";
		public static const axis_2d_stick:String = ".axis_2d_stick";
		public static const axis_2d_stick_line:String = ".axis_2d_stick_line";
		public static const axis_2d_labels:String = ".axis_2d_labels";
		
		public static const y_axis_2d:String = ".y_axis_2d";
		public static const y_axis_2d_title:String = ".y_axis_2d_title";
		public static const y_axis_2d_stick:String = ".y_axis_2d_stick";
		public static const y_axis_2d_stick_line:String = ".y_axis_2d_stick_line";
		public static const y_axis_2d_labels:String = ".y_axis_2d_label";
		
		private static var index_:uint = 0;
		
		public static const default_colors:String = ".colors";
		// The default file location.
		public static const common_css:String = "css/common.css";
		
		private var styleSheet:StyleSheet = new StyleSheet();
		
		private static var cssLoader:CSSLoader;
		
		public static function newInstance( file:String = common_css ):void
		{
			cssLoader = new CSSLoader(file);
			//return cssLoader;
		}
		
		public static function instance():CSSLoader
		{
			return cssLoader;
		}
		
		public function CSSLoader( file:String = common_css )
		{
			var loader:URLLoader = new URLLoader();
			
            loader.addEventListener(Event.COMPLETE, completeHandler);
            loader.addEventListener(Event.OPEN, openHandler);
            loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
            var request:URLRequest = new URLRequest(file);
            try {
                loader.load(request);
            } catch (error:Error) {
				exception();
            }
		}
		
		// Get the default color that have been defined in file.
		public function getRowColor():uint
		{
			var colors:Array = getDefaultColors();
			var color:uint = colors[index_ % 10];
			index_++;
			return color;
		}
		
		// The sytle defination file is loaded successfully.
		private function completeHandler(event:Event):void 
		{
            var loader:URLLoader = URLLoader(event.target);
			styleSheet.parseCSS(loader.data);
			dispatchEvent( new LoaderEvent(LoaderEvent.LOADED) );
        }
		
		// Get the style with name.
		public function getStyle( name:String ):Object
		{
			
			return styleSheet.getStyle(name);
			
		}
		
		// Parse a string to an object.
		public function parseStyle( styleString:String):void
		{
			//TODO: parse a string to an object
		}
		
		private function getDefaultColors():Array
		{
			var object:Object = styleSheet.getStyle(default_colors);
			var colors:Array = [object.color_1, object.color_2, object.color_3, object.color_4, object.color_5,
			              object.color_6, object.color_4, object.color_8, object.color_9, object.color_0];
			return colors;
		}
		
        private function openHandler(event:Event):void 
		{
           // trace("openHandler: " + event);
        }
		
		// Something wrong when load the style defination file.
        private function ioErrorHandler(event:IOErrorEvent):void 
		{
           dispatchEvent(new LoaderEvent(LoaderEvent.FAILED));
		}
		
		private function exception():void
		{
			dispatchEvent(new LoaderEvent(LoaderEvent.EXCEPTION));
		}

	}
	
}